﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace WXControl
{

    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("User32")]
        public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
        [DllImport("user32.dll")]
        static extern bool SetCursorPos(int X, int Y);

        public enum MouseEventFlags
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            Wheel = 0x0800,
            Absolute = 0x8000
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int ox = Control.MousePosition.X;
            int oy = Control.MousePosition.Y;

            int x = int.Parse(textBox1.Text);
            int y = int.Parse(textBox2.Text);
            SetCursorPos(x, y);//用屏幕取点工具可以得到坐标

            if (!checkBox1.Checked)
            {
                mouse_event((int)(MouseEventFlags.RightDown | MouseEventFlags.Absolute), x, y, 0, IntPtr.Zero);
                mouse_event((int)(MouseEventFlags.RightUp | MouseEventFlags.Absolute), x, y, 0, IntPtr.Zero);
            }
            else
            {
                mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), x, y, 0, IntPtr.Zero);
                mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), x, y, 0, IntPtr.Zero);

            }

            SetCursorPos(ox, oy);//用屏幕取点工具可以得到坐标

        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            //注册热键Shift+S，Id号为100。HotKey.KeyModifiers.Shift也可以直接使用数字4来表示。
            HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.None, Keys.F1);
            //注册热键Ctrl+B，Id号为101。HotKey.KeyModifiers.Ctrl也可以直接使用数字2来表示。
            HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.None, Keys.F2);
            //注册热键Alt+D，Id号为102。HotKey.KeyModifiers.Alt也可以直接使用数字1来表示。
            //HotKey.RegisterHotKey(Handle, 102, HotKey.KeyModifiers.Alt, Keys.D);
        }

        private void Form1_Leave(object sender, EventArgs e)
        {
            //注销Id号为100的热键设定
            HotKey.UnregisterHotKey(Handle, 100);
            //注销Id号为101的热键设定
            HotKey.UnregisterHotKey(Handle, 101);
            //注销Id号为102的热键设定
            //HotKey.UnregisterHotKey(Handle, 102);
        }

        ///重载FromA中的WndProc函数
        /// 
        /// 监视Windows消息
        /// 重载WndProc方法，用于实现热键响应
        /// 
        /// 
        protected override void WndProc(ref Message m)
        {

            const int WM_HOTKEY = 0x0312;
            //按快捷键 
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    switch (m.WParam.ToInt32())
                    {
                        case 100:    //按下的是Shift+S
                                     //此处填写快捷键响应代码 
                            button1_Click(null,null);
                            break;
                        case 101:    //按下的是Ctrl+B
                            textBox1.Text = Control.MousePosition.X.ToString();
                            textBox2.Text = Control.MousePosition.Y.ToString();

                            break;
                        case 102:    //按下的是Alt+D
                                     //此处填写快捷键响应代码
                            break;
                    }
                    break;
            }
            base.WndProc(ref m);
        }
    }

}
